<template>
  <el-dialog v-el-drag-dialog :title="!form.configId?'新建参数':'编辑参数'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="configForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item label="参数名称" prop="configName">
          <el-input v-model="form.configName" />
        </el-form-item>
        <el-form-item label="参数键名" prop="configKey">
          <el-input v-model="form.configKey" />
        </el-form-item>
        <el-form-item label="参数键值" prop="configValue">
          <el-input
            v-model="form.configValue"
            type="textarea"
            :rows="5"
            show-word-limit
            maxlength="300"
          />
        </el-form-item>
        <el-form-item label="系统内置" prop="configType">
          <el-select v-model="form.configType">
            <el-option
              v-for="item in optionOfType"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { configLoad, configSave} from '@/api/system/config'

export default {
  name: 'ConfigFormDialog',
  data() {
    const validateUniq = (rule, value, callback) => {
      if (value === undefined || value === '') {
        rule.message = '该项不能为空'
        return callback(new Error(rule.message))
      }
      const param = rule.param.replaceParam
      rule.param[param] = value
      rule.param.configId = this.form.configId
      this.$http.get(rule.url, {
        params: rule.param
      }).then(response => {
        if (response.valid) {
          callback()
        } else {
          callback(new Error(rule.message))
        }
      }).catch(() => {
        callback()
      })
    }

    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        configId: undefined,
        configName: undefined,
        configKey: undefined,
        configValue: undefined,
        configType: undefined
      },
      rules: {
        configName: [{
          required: true,
          trigger: 'blur',
          param: {
            replaceParam: 'configName'
          },
          validator: validateUniq,
          url: `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/config/checkUnique`,
          message: '已存在参数名称'
        }],
        configKey: [{
          required: true,
          trigger: 'blur',
          param: {
            replaceParam: 'configKey'
          },
          validator: validateUniq,
          url: `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/config/checkUnique`,
          message: '已存在参数键名'
        }],
        configValue: [{ required: true, trigger: 'none', message: '参数键值不能为空' }]
      },
      optionOfType: []
    }
  },
  methods: {
    init(configId) {
      this.hasLoaded = false
      this.form.configId = configId || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(configId)
        this.getDictData()
      })
    },
    loadForm(configId) {
      configLoad({ 'id': configId }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['tool:config:edit'])
      }).catch(() => {})
    },
    getDictData() {
      this.$dictGroup('sys_yes_no_char').then(response => {
        this.optionOfType = response
      }).catch(e => {})
    },
    onSubmit() {
      this.$refs.configForm.validate(valid => {
        if (valid) {
          configSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.configForm.resetFields()
      this.visible = false
    }
  }
}
</script>
